package com.yanqu.road.server.cmd.activity.washe;

import com.yanqu.road.entity.servercenter.ServerInfo;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.WasheProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.server.manger.cross3.GameXServerNameMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.Cross3Protocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

@Cmd(code = GameProtocol.S_WASHE_SEE_OTHER_USER_DATA, desc = "")
public class WasheSeeOtherUserDataCmd extends AbstractCommand {

    private WasheProto.WasheSeeOtherUserDataReqMsg reqMsg;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        reqMsg = WasheProto.WasheSeeOtherUserDataReqMsg.parseFrom(packet.getBytes());
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

        WasheProto.ChannelWasheSeeOtherUserDataReqMsg.Builder builder = WasheProto.ChannelWasheSeeOtherUserDataReqMsg.newBuilder();

        // 我方信息
        long serverId = GameServer.getInstance().getServerId();
        int channelId = ServerListMgr.getChannelId(serverId);
        builder.setFromChannelId(channelId);
        builder.setFromServerId(serverId);
        builder.setFromUserId(packet.getPlayerId());

        // 拿对方渠道
        int tempChannelId = 0;
        ServerInfo tempInfo = ServerListMgr.getServerInfo(reqMsg.getServerId());
        if (tempInfo != null) {
            tempChannelId = tempInfo.getChannelId();
        } else {
            tempChannelId = GameXServerNameMgr.getServerNameMap().getWasheChannelId(reqMsg.getServerId());
        }
        if (tempChannelId == 0) {
            ret = GameErrorCode.E_WASHE_CAN_NOT_SEE_OTHER;
            return;
        }

        // 对方信息
        builder.setSeeChannelId(tempChannelId);
        builder.setSeeServerId(reqMsg.getServerId());
        builder.setSeeUserId(reqMsg.getUserId());

        // 发送跨服请求
        player.sendPacket(Cross3Protocol.C3_WASHE_SEE_OTHER_USER_DATA, builder);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            WasheProto.WasheSeeOtherUserDataRespMsg.Builder builder = WasheProto.WasheSeeOtherUserDataRespMsg.newBuilder();
            builder.setRet(ret);
            builder.setOtherChannel(true);
            player.sendPacket(ClientProtocol.U_WASHE_SEE_OTHER_USER_DATA, builder);
        }
    }
}
